package org.pp.controller;


import cn.hutool.core.util.IdUtil;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.pp.config.JsapiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay/jsapi")
@Slf4j
public class PayJsApiController {

    @Autowired
    JsapiConfig jsapiConfig;

    @Resource
    RSAAutoCertificateConfig config;

    @GetMapping("/openid")
    @Operation(summary = "授权获得openid")
    public void openid(){


    }

    @GetMapping("/prepay")
    @Operation(summary = "测试公众号序预支付")
    public void prepay(){
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
        // 跟之前下单示例一样，填充预下单参数
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        //1分钱
        amount.setTotal(1);
        request.setAmount(amount);
        request.setAppid(jsapiConfig.getAppid());
        request.setMchid(jsapiConfig.getMerchantId());
        request.setDescription("商品:测试");
        request.setNotifyUrl(jsapiConfig.getNotify());
        request.setOutTradeNo(IdUtil.fastSimpleUUID());
        Payer payer= new Payer();
        payer.setOpenid("xxx");
        // response包含了调起支付所需的所有参数，可直接用于前端调起支付
        PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
    }
}
